package com.homedo.odin.solution.service.bizservice.personal;

import com.homedo.core.common.base.dto.Meta;
import com.homedo.core.common.exception.BizException;
import com.homedo.odin.solution.contract.pc.GetSolProductDetailListResp;
import com.homedo.odin.solution.model.dto.Tree;
import com.homedo.odin.solution.model.entity.SolProductDetail;
import com.homedo.odin.solution.model.entity.SolProductList;
import com.homedo.odin.solution.service.SolProductDetailServiceImpl;
import com.homedo.odin.solution.service.SolProductListServiceImpl;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author: donglibing
 * @date: 2018/3/7 18:58
 * @Description: 信息业务处理
 */
@Service
public class SolProductDetailBizService {

    @Autowired
    SolProductListServiceImpl solProductListService1;

    @Autowired
    SolProductDetailServiceImpl solProductDetailService;

    public GetSolProductDetailListResp getSolProductDetailList(String id) {
        GetSolProductDetailListResp resp = new GetSolProductDetailListResp();

        Map<String, Object> map = new HashMap<>();
        map.put("solution_id", id);
        map.put("is_del", 0);
        map.put("status", 1);
        List<SolProductList> solProductListList = solProductListService1.selectByMap(map);
        if (solProductListList == null || solProductListList.size() == 0) {
            throw new BizException(Meta.CODE_451.getCode(), "未查询到方案产品清单");
        }

        Map<String, Object> map1 = new HashMap<>();
        map1.put("list_id", solProductListList.get(0).getId());
        map1.put("is_del", 0);
        List<SolProductDetail> solProductDetailList = solProductDetailService.selectByMap(map1);
        List<Tree> trees = convertToTree(solProductDetailList);
        // List<SolProductDetailBean> solProductDetailBeanList = ObjectConverter.copyList(solProductDetailList, SolProductDetailBean.class);
        resp.setSolProductDetailBeanList(trees);
        resp.setListId(solProductListList.get(0).getId());
        return resp;
    }

    //转化为树状结构
    private List<Tree> convertToTree(List<SolProductDetail> list) {

        List<Tree> oneLevelTree = new ArrayList<>();
        List<Tree> TwoLevelTree = new ArrayList<>();
        List<Tree> ThreeLevelTree = new ArrayList<>();

        List<String> levelName = new ArrayList<>();
        List<String> levelName1 = new ArrayList<>();
        if (list != null && list.size() > 0) {
            List<Tree> tr = new ArrayList<>();
            for (SolProductDetail re : list
                    ) {

                if (!levelName1.contains(re.getSysType()) || !levelName.contains(re.getSysSort())) {
                    Tree tree2 = new Tree();
                    tree2.setLevelName(re.getSysType());
                    tree2.setParentLevelName(re.getSysSort());
                    TwoLevelTree.add(tree2);
                    levelName1.add(re.getSysType());
                }

                if (!levelName.contains(re.getSysSort())) {
                    Tree tree1 = new Tree();
                    tree1.setLevelName(re.getSysSort());
                    oneLevelTree.add(tree1);
                    levelName.add(re.getSysSort());
                }

                Tree tree3 = new Tree();
                tree3.setParentLevelName(re.getSysType());
                tree3.setUnit(re.getUnit());
                tree3.setSkuId(re.getSkuId());
                tree3.setProductName(re.getProductName());
                tree3.setPrice(re.getPrice());
                tree3.setModel(re.getModel());
                tree3.setBrand(re.getBrand());
                tree3.setAmount(re.getAmount());
                tree3.setGrendParentLevelName(re.getSysSort());
                tree3.setListId(re.getListId());
                ThreeLevelTree.add(tree3);

            }
            //初始化二级
            for (Tree t : TwoLevelTree) {
                List<Tree> child = new ArrayList<>();
                for (Tree t1 : ThreeLevelTree
                        ) {
                    if (t.getLevelName().equals(t1.getParentLevelName()) && t.getParentLevelName()
                            .equals(t1.getGrendParentLevelName())) {
                        child.add(t1);

                    }
                }
                t.setChild(child);
            }
            //初始化一级
            for (Tree t : oneLevelTree) {
                List<Tree> child = new ArrayList<>();
                for (Tree t1 : TwoLevelTree
                        ) {
                    if (t.getLevelName().equals(t1.getParentLevelName())) {
                        child.add(t1);

                    }
                }
                t.setChild(child);
            }

            return oneLevelTree;
        }
        return null;

    }


}